﻿@model IEnumerable<Configuration>
@{
    ViewBag.Title = this.GetTtile("Configuration");
}
@section head{
    <script type="text/javascript" src="@this.Static("~/Scripts/jquery.validate.min.js")"></script>
    <script type="text/javascript" src="@this.Static("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
    <script type="text/javascript" src="@this.Static("~/Scripts/ckeditor/ckeditor.js")"></script>
    <script type="text/javascript" src="@this.Static("~/Scripts/ckeditor/adapters/jquery.js")"></script>
}
<article>
    @using (Html.BeginForm("Save", "Configuration"))
    {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Post</legend>
            @foreach (var config in Model)
            {
                <div class="editor-label">
                    @Html.Label(config.ConfigKey)
                </div>
                <div class="editor-field">
                    @if (!config.TextArea)
                    {
                        @Html.TextBox(config.ConfigKey, config.ConfigValue)
                    }
                    else
                    {
                        @Html.TextArea(config.ConfigKey, config.ConfigValue)
                    }
                </div>
            }
            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>
    }
</article>
<script type="text/javascript">
    $(document).ready(function () {
        $("textarea").each(function () {
            var textName = $(this).attr('name');
            //Init editor
            CKEDITOR.replace(textName,
            {
                language: '@this.UICulture.Substring(0, 2)'
            });
            var editorCke = CKEDITOR.instances[textName];
            //Set ignore to empty. the field to validate is hidden
            $(editorCke.element.$).closest('form').validate().settings.ignore = "";
            //When editor lose focus
            editorCke.on('blur', function (e) {
                //Update element and the form validation
                e.editor.updateElement();
                $(e.editor.element.$).valid();
            });
        });
    });
</script>
